<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('positions', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('department_id')->comment('所属部门ID');
            $table->string('name', 100)->comment('岗位名称');
            $table->string('code', 50)->unique()->comment('岗位编码');
            $table->text('description')->nullable()->comment('岗位描述');
            $table->enum('level', ['junior', 'middle', 'senior', 'manager', 'director', 'vp', 'ceo'])->comment('职级');
            $table->enum('category', ['administrative', 'technical', 'sales', 'operation', 'other'])->comment('岗位类别');
            $table->json('responsibilities')->nullable()->comment('岗位职责');
            $table->json('requirements')->nullable()->comment('岗位要求');
            $table->integer('headcount')->default(1)->comment('编制人数');
            $table->integer('current_count')->default(0)->comment('当前人数');
            $table->enum('status', ['active', 'inactive', 'frozen'])->default('active')->comment('岗位状态');
            $table->integer('sort_order')->default(0)->comment('排序权重');
            $table->timestamps();
            
            // 外键约束
            $table->foreign('department_id')->references('id')->on('departments')->onDelete('cascade');
            
            // 索引
            $table->index(['department_id', 'status']);
            $table->index(['code', 'status']);
            $table->index(['name', 'status']);
            $table->index(['level', 'category']);
            $table->index('sort_order');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('positions');
    }
};
